package com.tojabo.link;

public class FirstLastList {

	private Link first;
	private Link last;

	public FirstLastList() {
		first = null;
		last = null;
	}

	public boolean isEmpty() {
		return first == null;
	}

	public void insertFirst(long dd) {
		Link newLink = new Link(dd);

		// 1、链表是空的情况
		if (isEmpty()) {
			first = newLink;
			last = newLink;
		} else {
			// 2、有链的情况
			newLink.next = first;
			first = newLink;
		}
	}

	public void insertLast(long dd) {
		Link newLink = new Link(dd);

		// 1、链表是空的情况
		if (isEmpty()) {
			first = newLink;
			last = newLink;
		} else {
			last.next = newLink;
			last = newLink;
		}
	}

	public long deleteFirst() {
		long temp = first.dData;

		if (first.next == null) {
			last = null;
		}
		first = first.next;
		return temp;
	}

	public void displayList() {
		System.out.print("List (first-->last): ");
		Link current = first;

		while (current != null) {
			current.displayLink();
			current = current.next;
		}
		System.out.println("");
	}

	class Link {

		public long dData;
		public Link next;

		public Link(long d) {
			dData = d;
		}

		public void displayLink() {
			System.out.print(dData + " ");
		}

	}
}
